package com.yunxi.controller.eai.test;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/5/9.
 */
public class ClientMain {
    @SuppressWarnings("unused")
    public static void main(String[] args) {
        //http://10.64.19.98:7080/esb/ehr/api
       // String url ="http://10.64.19.85:7080/esb/ehr/api";
        //String url ="http://127.0.0.1:9080/esb/api/receive";
       //String url ="http://192.168.1.52:9080/esb/api/receive";
        String url ="http://39.106.227.70:9080/esb/api/receive";       //String url ="http://10.16.17.45:9080/esb/api/receive";
       // String jsonStr ="{\"StartTime\":\"2018-01-28T06:07:31.660Z\",\"StopTime\":\"2019-05-29T06:07:31.660Z\",\"WithDisabled\":true,\"WithDeleted\":true,\"PageIndex\":1,\"PageSize\":10,\"Sort\":{},\"ExtParams\":{},\"Columns\":[\"Name\"]}";
  /*      File file = new File("F:/data/bom02.txt");
        String jsonStr =txt2String(file);*/

/*String jsonStr ="{\n" +
                "\t\"userId\": \"4960\",\n" +
                "\t\"comment\": \"测试驳回\",\n" +
                "\t\"orderNo\": \"BL20190911671988\",\n" +
                "\t\"refundStatus\": \"4\",\n" +
                "\t\"refundOrderNo\": \"111111\"\n" +
             "}";*/
          /* String jsonStr ="{\n" +
                "\t\"certNo\": \"420682199203096339\",\n" +
                "\t\"buyType\": \"1\",\n" +
                "\t\"hubCode\": \"\",\n" +
                "\t\"orderNo\": \"BL20190917503039\",\n" +
                "\t\"bfAmount\": \"\",\n" +
                "\t\"blAmount\": \"0.05\",\n" +
                "\t\"certType\": \"1\",\n" +
                "\t\"cityCode\": \"1201\",\n" +
                "\t\"fellowId\": \"\",\n" +
                "\t\"colorCode\": \"WB01\",\n" +
                "\t\"customerId\": \"4976\",\n" +
                "\t\"fellowName\": \"\",\n" +
                "\t\"outTradeNo\": \"BL201909175025161568703422739\",\n" +
                "\t\"companyCode\": \"\",\n" +
                "\t\"companyName\": \"\",\n" +
                "\t\"energyPoint\": \"0\",\n" +
                "\t\"featureCode\": \"XZAB\",\n" +
                "\t\"invitorName\": \"\",\n" +
                "\t\"orderAmount\": \"240000.00\",\n" +
                "\t\"orderStatus\": \"2\",\n" +
                "\t\"partnerCode\": \"\",\n" +
                "\t\"totalAmount\": \"0.03\",\n" +
                "\t\"customerName\": \"陈阿蒙\",\n" +
                "\t\"fellowAvatar\": \"\",\n" +
                "\t\"fellowGender\": \"\",\n" +
                "\t\"fellowMobile\": \"\",\n" +
                "\t\"interiorCode\": \"NNGA\",\n" +
                "\t\"localSubsidy\": \"\",\n" +
                "\t\"orderSubject\": \"U5PRO+\",\n" +
                "\t\"stateSubsidy\": \"\",\n" +
                "\t\"invitorMobile\": \"\",\n" +
                "\t\"vehicleStatus\": \"\",\n" +
                "\t\"customerMobile\": \"18674521469\",\n" +
                "\t\"batteryPackCode\": \"\",\n" +
                "\t\"customerAddress\": \"\",\n" +
                "\t\"productTypeCode\": \"AM861BFLCHL19B0\"\n" +
                "}";*/
       // String jsonStr="{\"vin\": \"VIN19092000636\", \"carRoad\": null, \"loadCode\": \"NAC201909210051\", \"createTime\": \"2019-09-21 15:21:00\", \"driverName\": \"殷海超\", \"carrierCode\": \"MSL\", \"carrierName\": \"民生\", \"deliveryCode\": \"bigBrid\", \"shippingTime\": \"2019-09-21 15:11:00\", \"driverCellPhoneNumber\": \"15850676124\", \"transportVehiclePlate\": \"苏AS56X9\"}";
        String jsonStr="{\n" +
                "\t\"certNo\":\"410426199402016666\",\n" +
                "\t\"address\":\"金水区\",\n" +
                "\t\"dlUrl\":\"http://baidu.com\",\n" +
                "\t\"phone\":\"18612359863\",\n" +
                "\t\"cityCode\":\"1201\",\n" +
                "\t\"name\":\"许冠\",\n" +
                "\t\"trailTime\":\"2019-11-11 00:00:00\",\n" +
                "\t\"trailType\":1,\n" +
                "\t\"userId\":\"440086\"\n" +
                "}";
         //String jsonStr="";
        String contentType = "application/json";
        String res=ClientMain.sendPost(url,jsonStr,contentType);
        System.out.println("返回信息:"+res);

       /* Map map=new HashMap();
        Map SortMap=new HashMap();
        Map ExtParamsMap=new HashMap();
        List list=new ArrayList();
        list.add("Name");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String time = sdf.format(new Date());
        System.out.println(time);
        map.put("StartTime",time);
        map.put("StopTime","2019-01-29T06:07:31.660Z");
        map.put("WithDisabled",true);
        map.put("WithDeleted",true);
        map.put("PageIndex",1);
        map.put("PageSize",10);
        map.put("Sort",SortMap);
        map.put("ExtParams",ExtParamsMap);
        map.put("Columns",list);
        Object json = JSONObject.toJSON(map);
        System.out.println(json.toString());
*/


    }

        /**
         * 读取txt文件的内容
         * @param file 想要读取的文件对象
         * @return 返回文件内容
         */
        public static String txt2String(File file){
            StringBuilder result = new StringBuilder();
            try{
                BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
                String s = null;
                while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                    result.append(System.lineSeparator()+s);
                }
                br.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            return result.toString();
        }

    public static String sendPost(String url,String bodyStr,String contentType) {
        StringBuffer resultBuffer = null;
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", contentType);
        httpPost.addHeader("requestId", "35b8d072-baae-11e6-9e71-0a0a012d6555");

      // httpPost.addHeader("serviceName", "S0220023A");
     // httpPost.addHeader("serviceName", "S0220017A");
      // httpPost.addHeader("serviceName", "S0220010A");
        httpPost.addHeader("serviceName", "S0220021A");

        httpPost.addHeader("sourceSystem", "APP");
        BufferedReader br = null;
        try {
            StringEntity entity = new StringEntity(bodyStr, "UTF-8");
            httpPost.setEntity(entity);
            HttpResponse response = client.execute(httpPost);
            //获取头信息
           Map map= getHeader(response);
            resultBuffer = new StringBuffer();
            br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String temp;
            while ((temp = br.readLine()) != null) {
                resultBuffer.append(temp);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    br = null;
                    throw new RuntimeException(e);
                }
            }
        }
        return resultBuffer.toString();
    }

    private static Map getHeader(HttpResponse response) {
        Map map=new HashMap();
        Header[] allHeaders = response.getAllHeaders();
        for (int i = 0; i < allHeaders.length; i++) {
            Header allHeader = allHeaders[i];
            map.put(allHeader.getName(),allHeader.getValue());
        }
        System.err.println(map);
        return map;
    }
}
